package com.Sinewecology.EntropyWechat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.entrogy.AccessToken.AccessTokenUpdate;
import com.entrogy.bean.PubCountEntity;
import com.entrogy.utils.StrUtils;
import org.apache.http.ParseException;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author xlr
 */
public class WX_UserUtil {
    private static Logger logger = LoggerFactory.getLogger(WX_UserUtil.class);
	/**
	 * ����΢��openId ��ȡ�û��Ƿ���
	 * 
	 * @param ΢��openId
	 * @return �Ƿ��ĸù��ںű�ʶ
	 * @throws IOException
	 * @throws ParseException
	 */
	public static JSONObject packJsonmsg(Map<String, TemplateData> param) {
		JSONObject json = new JSONObject();
		for (Map.Entry<String, TemplateData> entry : param.entrySet()) {
			JSONObject keyJson = new JSONObject();
			TemplateData dta = entry.getValue();
			keyJson.put("value", dta.getValue());
			keyJson.put("color", dta.getColor());
			json.put(entry.getKey(), keyJson);
		}
		return json;
	}

	public static Integer subscribeState(PubCountEntity pubCountEntity) throws ParseException, IOException {
		String tmpurl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + pubCountEntity.getAccessToken()
				+ "&openid=" + pubCountEntity.getOpenid();
		JSONObject result = WX_HttpsUtil.httpsRequest(tmpurl, "GET", null);
		JSONObject resultJson = new JSONObject(result);
//        log.error("��ȡ�û��Ƿ��� errcode:{} errmsg:{}", resultJson.getInteger("errcode"), resultJson.getString("errmsg"));
		String errmsg = (String) resultJson.get("errmsg");
		if (errmsg == null) {
			// �û��Ƿ��ĸù��ںű�ʶ��0������û�û�й�ע�ù��ں� 1��ʾ��ע�˸ù��ںţ���
			Integer subscribe = (Integer) resultJson.get("subscribe");
			return subscribe;
		}
		return -1;
	}

	public static List<String> getUsrList(String accessToken) throws ParseException, IOException {
		List<String> usrList = new ArrayList<String>();
		String tmpurl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken;
		JSONObject result = WX_HttpsUtil.httpsRequest(tmpurl, "GET", null);
		JSONObject resultJson = new JSONObject(result);
		String errmsg = (String) resultJson.get("errmsg");
		if (errmsg == null) {
			JSONObject openIdList = (JSONObject) resultJson.get("data");
			JSONArray s = openIdList.getJSONArray("openid");// .getString("openid");

			for (int i = 0; i < s.size(); i++) {
				usrList.add(s.get(i).toString());
			}
			return usrList;
		}
		return null;
	}

	public static PubCountEntity getPubCountUsrInformation(String token, String openid)
			throws ParseException, IOException {
		PubCountEntity pcb = new PubCountEntity();
		String tmpurl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + token + "&openid=" + openid
				+ "&lang=zh_CN";
		JSONObject result = WX_HttpsUtil.httpsRequest(tmpurl, "GET", null);
		JSONObject resultJson = new JSONObject(result);
		String errmsg = (String) resultJson.get("errmsg");
		if (StrUtils.isNullOrEmpty(errmsg)) {
			pcb.setOpenid(resultJson.get("openid").toString());
			pcb.setUnionid(resultJson.get("unionid").toString());
			logger.info("Opneid:" + pcb.getOpenid());
			logger.info("Unionid:" + pcb.getUnionid());
		}
		return pcb;
	}

}
